package models

import (
	"0411zk/common/initialize"
	study "0411zk/common/proto"
	"github.com/google/uuid"
	"gorm.io/gorm"
	"time"
)

// todo 用户表
type User struct {
	gorm.Model
	Mobile   string `gorm:"type:varchar(20);not null;index:ip_idx common:'用户电话'"`
	Password string `gorm:"type:varchar(32);not null;index:ip_idx common:'用户密码'"`
}

func (u User) FindUserMobile(mobile string) (user *User, err error) {
	err = initialize.DB.Where("mobile = ?", mobile).Take(&user).Error
	if err != nil {
		return nil, err
	}
	return
}

func (u User) CreateUser(req *study.UserRegisterReq) (user *User, err error) {

	user = &User{
		Mobile:   req.Mobile,
		Password: req.Password,
	}

	tx := initialize.DB.Begin()
	// 注意，一旦你在一个事务中，使用tx作为数据库句柄

	err = tx.Create(&user).Error

	if err != nil {
		tx.Rollback()
		return nil, err
	}

	userCoupon := &UserCoupon{
		UserId:   int(user.ID),
		CouponId: 1,
		Status:   0,
	}

	err = tx.Create(&userCoupon).Error

	if err != nil {
		tx.Rollback()
		return nil, err
	}

	tx.Commit()
	return
}

func (u User) CreateUsers(mobile string, password string) (user *User, err error) {
	user = &User{
		Mobile:   mobile,
		Password: password,
	}

	tx := initialize.DB.Begin()
	// 注意，一旦你在一个事务中，使用tx作为数据库句柄

	err = tx.Create(&user).Error

	if err != nil {
		tx.Rollback()
		return nil, err
	}

	userCoupon := &UserCoupon{
		UserId:   int(user.ID),
		CouponId: 1,
		Status:   0,
	}

	err = tx.Create(&userCoupon).Error

	if err != nil {
		tx.Rollback()
		return nil, err
	}

	tx.Commit()
	return
}

// todo 优惠卷表
type Coupon struct {
	gorm.Model
	Name  string  `gorm:"type:varchar(20);not null;index:ip_idx common:'优惠卷名字'"`
	Full  float64 `gorm:"type:decimal(10,2);not null;index:ip_idx common:'满多少'"`
	Minus float64 `gorm:"type:decimal(10,2);not null;index:ip_idx common:'减多少'"`
}

func (c Coupon) FindCoupon(id int64) (course *Coupon, err error) {
	err = initialize.DB.Where("user_id=?", id).Take(&course).Error
	if err != nil {
		return nil, err
	}
	return

}

// todo 用户关联优惠卷表
type UserCoupon struct {
	gorm.Model
	UserId   int `gorm:"type:int;not null;index:ip_idx common:'用户ID'"`
	CouponId int `gorm:"type:int;not null;index:ip_idx common:'优惠卷ID'"`
	Status   int `gorm:"type:int;not null;index:ip_idx common:'使用状态0:未使用 1已使用'"`
}

// todo 课程表
type Course struct {
	gorm.Model
	Name             string  `gorm:"type:varchar(20);not null;index:ip_idx common:'课程名字'"`
	Price            float64 `gorm:"type:decimal(10,2);not null;index:ip_idx common:'课程价格'"`
	Stock            int64   `gorm:"type:int;not null;index:ip_idx common:'课程数量'"`
	ClassificationID int64   `gorm:"type:int;not null;index:ip_idx common:'分类ID'"`
}

func (c Course) FindCoursesList() (course []*Course, err error) {
	err = initialize.DB.Find(&course).Error
	if err != nil {
		return nil, err
	}
	return
}

func (c Course) FindCourses(id int64) (course *Course, err error) {
	err = initialize.DB.Raw("SELECT * FROM users JOIN user_coupons ON users.id=user_coupons.id").Scan(&course).Error
	if err != nil {
		return nil, err
	}
	return
}

// todo 学习计划表
type StudyPlan struct {
	gorm.Model
	UserID   int `gorm:"type:int;not null;index:ip_idx common:'用户ID'"`
	CourseId int `gorm:"type:int;not null;index:ip_idx common:'课程ID'"`
}

func (p StudyPlan) CreateCourse(id *study.BrowseCoursesAddReq) (play *StudyPlan, err error) {

	play = &StudyPlan{

		UserID:   int(id.UserId),
		CourseId: int(id.CoursesId),
	}

	err = initialize.DB.Create(&play).Error
	if err != nil {
		return nil, err
	}
	return
}

func (p StudyPlan) DelCourse(req *study.BrowseCoursesDelReq) (play *StudyPlan, err error) {
	err = initialize.DB.Model(&play).Where("course_id = ? ", req.CoursesId).Delete("course_id").Error
	if err != nil {
		return nil, err
	}
	return
}

// todo 课程订单表
type CourseOrder struct {
	gorm.Model
	UserId   int64   `gorm:"type:int;not null;index:ip_idx common:'用户ID'"`
	CourseId int64   `gorm:"type:int;not null;index:ip_idx common:'课程ID'"`
	OrderSn  string  `gorm:"type:varchar(255);not null;index:ip_idx common:'订单编号'"`
	Sum      int64   `gorm:"type:int;not null;index:ip_idx common:'订单数量'"`
	Price    float64 `gorm:"type:decimal(10,2);not null;index:ip_idx common:'订单价格'"`
	Status   int64   `gorm:"type:int;not null;index:ip_idx common:'订单状态'"`
	Pay      int64   `gorm:"type:int;not null;index:ip_idx common:'支付方式'"`
}

func (o CourseOrder) CreateOrder(req *study.CoursesOrderReq, c *Course) (order *CourseOrder, err error) {

	now := time.Now().Format(time.DateTime)

	orderSn := uuid.New().String()

	order = &CourseOrder{
		UserId:   req.UserId,
		CourseId: req.CoursesId,
		OrderSn:  now + orderSn,
		Sum:      req.Sum,
		Price:    (float64(req.Sum) * c.Price) - float64(5),
		Status:   0,
		Pay:      req.Pay,
	}

	err = initialize.DB.Create(&order).Error
	if err != nil {
		return nil, err
	}

	return order, nil
}
